[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
malloc() Allocate Memory Block
#include <stdlib.h> Required for declarations only
#include <alloc.h>
void *malloc(size);
size_t size; Number of bytes to allocate
malloc() allocates a memory block of 'size' bytes.
Returns: Pointer to allocated space. Returns NULL (defined in
<stdio.h>) if the space cannot be allocated. The
contents of the block are not changed. If 'size' ==
0, NULL is returned.
Notes: Use free() to deallocate a block allocated with
malloc().
The block allocated by malloc() may be larger than
'size' bytes because of space required for alignment
and DOS housekeeping. The space is guaranteed to be
suitably aligned for storage of any type of object.
-------------------------------- Example ---------------------------------
The following statements allocate space for 1000 bytes and then free
the allocated space.
#include <stdlib.h>
#include <stdio.h> /* for printf and NULL */
char *memptr;
main()
{
if ((memptr = malloc(1000)) == NULL)
printf("not enough room to allocate memory\n");
else {
.
.
free(memptr);
}
}
See Also:
calloc()
realloc()
free()
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson